class Contest < ActiveRecord::Base
  has_and_belongs_to_many :problems

  validate :start_time_must_be_before_end_time
  validates_presence_of :name, :start_time, :end_time
  validates_uniqueness_of :name
  
#  validates_uniqueness_of :problems
# Better restrain from making the sane assumption
# that the start time should be in the future
#  validate :start_and_cannot_be_in_the_past, :on => :create

  def start_and_cannot_be_in_the_past
    errors.add(:start_time, "cannot be in the past") if start_time and Time.now >= start_time
  end

  def start_time_must_be_before_end_time
    errors.add(:start_time, "must be before end time") if start_time and end_time and start_time >= end_time
  end
end
